home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-04-19 | 12.9 KB | 552 lines | [TEXT/MMCC] |
- //---------------------------------------------------------------------
- //---------------------------------------------------------------------
- //
- // Horrible Rickety Shell, by Dave Johnson
- //
- // © Copyright 1985 - 1995 Anyone Who Wants It,
- // All Rights Energetically Hurled as far away from me as possible.
- // Use at your own (considerable) risk.
-
-
- #include "PaperJuggling.h"
- #include <Folders.h>
- #include <Scrap.h>
-
- // The shell globals we need
- extern MenuHandle gShellMenuHandles[];
- extern short gDocTitleHeight, gDocFrameWidth;
-
- // App Globals
- MenuHandle gAppMenuHandles[kNumAppMenus]; // The menus we add to the shell
-
- // GX globals
- Boolean gDebugging = true; // If gDebugging = TRUE, errors and notices will be posted.
- Boolean gGiveMeValidation = true; // if gGiveMeValidation = TRUE, run-time validation occurs.
- long gGraphicsHeapSize = 600; // pretty big
- GXPrintingEventUPP gOurPrintingOverrideUPP;
- gxGraphicsClient gGraphicsClient;
-
- // Called by the Shell at startup time
- Boolean AppInit(void)
- {
- MenuHandle mhndl;
-
- // Add the "Juggle" menu
- mhndl = GetMenu(kJuggleMenuID);
- if(mhndl == nil)
- {
- DoErrorAlert(kNoResStr, 0);
- return false;
- }
- (*mhndl)->menuID = kJuggleMenuID;
- InsertMenu(mhndl, 0);
- DisableItem(mhndl, 0); // Disable the menu until user opens a file
- gAppMenuHandles[kJuggleMenu] = mhndl;
-
- DrawMenuBar();
-
- // Initialize global graphic objects
- InitCommonGraphicObjects();
- return(true);
- }
-
- // A chance to pre-handle an event
- Boolean AppDoEvent(EventRecord *event)
- {
- Boolean handled = false;
- CursHandle curs;
-
- // !!! be more efficient here
- // Adjust cursor
- if(event->modifiers & optionKey)
- {
- curs = GetCursor(kThumbCursorID);
- SetCursor(*curs);
- }
- else
- SetCursor(&qd.arrow);
-
- return handled;
- }
-
- // Called to handle an activate event
- void AppActivate(WindowPtr wind, Boolean activate)
- {
- }
-
- // Called when a window needs updating. BeginUpdate() has already been called, and the
- // port is set to the appropriate window
- void AppUpdate(WindowPtr wind)
- {
- // Draw the content
- AppDrawContent(wind);
- }
-
- // Draw the content now, assuming the port, clip, etc. are all set up
- void AppDrawContent(WindowPtr wind)
- {
- // Draw the page shape
- GXDrawShape(GetWindowGXShape(wind));
- GXDrawShape (GetDocFadedThrowsPict(wind));
- }
-
- // Called when the shell recieves a null event.
- void AppIdle(EventRecord *Event)
- {
- }
-
- /* Called when there is a click in the content of a window. The port is already set to
- the window, and thePt is in local coords. */
- void AppClick(Point thePt, WindowPtr wind, Boolean doubleClick, EventRecord *eventptr)
- {
- DoJuggleClick(wind, thePt, eventptr);
- }
-
- // Called when there is a keyDown event
- void AppKeyDown(char key, EventRecord *eventptr)
- {
- }
-
- // Called after the user has resized a window. Don't forget to account for the scroll bars
- void AppGrowWindow(WindowPtr wind, short hSize, short vSize)
- {
- }
-
- /* Called when the user clicks in the zoom box of a window. It calls a routine to find
- and install the appropriate zoom rect, then zooms it, adjusting the scroll bars and so
- on */
- void AppZoomWindow(WindowPtr wind, short zoomDir)
- {
- }
-
- /* Called when there is a click in the menu bar, before the menu is shown. This is
- the app's opportunity to enable and disable its menu items. */
- void AppAdjustMenus()
- {
- MenuHandle mhndl;
- Boolean windowUp;
-
- windowUp = (FrontWindow() != nil && IsAppWindow(FrontWindow()));
- mhndl = gAppMenuHandles[kJuggleMenu];
-
- // OK, first the Juggle menu. If a window is up, then enable it
- if(windowUp)
- EnableItem(mhndl, 0);
- else
- DisableItem(mhndl, 0);
-
- // Patterns Menu is always enabled
-
- // Make sure to show results
- DrawMenuBar();
- }
-
- // called when a menu other than Apple, File, or Edit is used.
- void AppMenu(short menuID, short menuItem)
- {
- WindowPtr wind = FrontWindow();
-
- switch(menuID)
- {
- case kJuggleMenuID:
- switch(menuItem)
- {
- case iResizeJuggle:
- wind = ResizeJuggle(wind);
- if(wind != nil)
- {
- SetPort(wind);
- EraseRect(&wind->portRect);
- InvalRect(&wind->portRect);
- }
- else
- SysBeep(10);
- break;
-
- case iAddJuggler:
- if(AddJuggler(GetWindowJuggle(wind)))
- {
- SetPort(wind);
- EraseRect(&wind->portRect);
- InvalRect(&wind->portRect);
- AdjustScrollbars(wind, false);
- }
- else
- SysBeep(10);
- break;
-
- default:
- break;
- }
- break;
-
- default:
- break;
- }
- }
-
- // Called when the user selects "New" from the File menu, and at other various times
- WindowPtr AppNew(short numJugglers, short numCounts)
- {
- WindowPtr wind = nil;
- OSErr err = -1;
- DocHandle newDoc;
- CursHandle curs;
-
- // If no size specified, ask user how big to make it
- if(numJugglers <= 0)
- DoJuggleSizeDialog(&numJugglers, &numCounts);
-
- // if user canceled, return
- if(numJugglers == 0)
- return nil;
-
- // Set cursor to watch
- curs = GetCursor(watchCursor);
- SetCursor(*curs);
-
- // Get a new window
- wind = GetNewWindow(kWindResourceID, nil, (WindowPtr)-1L);
- if(wind != nil)
- {
- // Allocate the document structure
- newDoc = (DocHandle)NewHandleClear(sizeof(DocumentRecord));
- if(newDoc != nil)
- {
- // store the doc in the window
- SetWRefCon(wind, (long)newDoc);
-
- // Initialize the standard document stuff
- err = InitStdDoc(wind);
- if(err == noErr)
- {
- // Try to set up the juggle.
- err = InitJuggle(wind, numJugglers, numCounts);
- if(err == noErr)
- {
- // Finally, position and show the window
- SetUpAndShowWindow(wind);
- }
- }
- }
- }
-
- // Clean up if there were errors
- if(err != noErr)
- {
- DoErrorAlert(kNoMemStr, 0);
- if(newDoc != nil)
- {
- DisposeHandle((Handle)newDoc);
- newDoc = nil;
- }
- if(wind != nil)
- {
- DisposeWindow(wind);
- wind = nil;
- }
- }
-
- // Set cursor back to arrow
- SetCursor(&qd.arrow);
-
- // return window pointer (possibly nil)
- return wind;
- }
-
- /* Called when the user selects "Open" from the File menu. */
- void AppOpen(void)
- {
- SFTypeList typeList;
- short err;
- StandardFileReply reply;
-
- /* only show JUGL files */
- typeList[0] = kJuggleFileType;
-
- /* Standard File Dialog, system 7 style */
- StandardGetFile(nil, 1, typeList, &reply);
-
- /* If they clicked "open"... */
- if(reply.sfGood == true)
- {
- /* A big gnarly routine that sets everything up and reads in the juggle */
- err = OpenJuggleFile(&reply.sfFile, nil);
- if(err != noErr)
- SysBeep(10);
- }
- }
-
-
- // Called when the window should be closed, assume saving has happened, if necessary
- void AppClose(WindowPtr wind)
- {
- DocHandle doc;
-
- doc = GetWindowDoc(wind);
- if(doc != nil)
- {
- if((*doc)->docPage != nil)
- GXDisposeShape((*doc)->docPage); // Dispose of this doc's shape.
- if((*doc)->docJob != nil)
- GXDisposeJob((*doc)->docJob); // Dispose of this doc's print job.
-
- // Clean up the juggle bits
- CleanUpJuggle(wind, true);
-
- // Finally kill the doc/juggle itself
- DisposeHandle((Handle)doc);
- }
- DisposeWindow(wind);
- }
-
- /* Called when the user selects "Save" from the File menu. Returns a boolean that is
- false only if AppSaveAs() is called and the user cancels the save, or if there is an
- error. */
- Boolean AppSave(WindowPtr wind)
- {
- OSErr err = noErr;
- Boolean success = true;
- JuggleHandle aJuggle;
-
- /* Get the juggle in question */
- aJuggle = GetWindowJuggle(wind);
- if(aJuggle == nil)
- return true; // means "cancelled"
-
- /* Check the file name in our window's spec. If empty, do save as, else save */
- if((*aJuggle)->fileSpec.name[0] == 0)
- {
- success = AppSaveAs(wind);
- }
- else /* a regular save, it's been saved before */
- {
- /* Write the data to the file */
- err = SaveJuggle(aJuggle);
-
- /* It's possible that the user switched to the finder and deleted the
- file or ejected the disk it's on, so let's check the error. If it's
- fnfErr or nsvErr, do a SaveAs */
- if(err == fnfErr || err == nsvErr)
- {
- err = noErr;
- success = AppSaveAs(wind);
- }
-
- if(err == noErr)
- /* Successful save, so set dirty flag to false */
- (*aJuggle)->dirty = false;
- else
- {
- success = false;
- // SysBeep(10);
- }
- }
- return success;
- }
-
- /* Called when the user selects "Save As..." from the File menu. Returns a boolean that
- is true if the save ends successfuly */
- Boolean AppSaveAs(WindowPtr wind)
- {
- Str255 title, prompt = "\pSave this pattern as:";
- StandardFileReply reply;
- OSErr err = noErr;
- Boolean success = false;
- JuggleHandle aJuggle;
-
- /* Get the juggle in question */
- aJuggle = GetWindowJuggle(wind);
- if(aJuggle == nil)
- return true; // !!!
-
- // Get the title
- GetWTitle(wind, title);
-
- /* put up Standard File Dialog */
- StandardPutFile(prompt, title, &reply);
-
- /* If user clicked Save, do it */
- if(reply.sfGood == true)
- {
- /* If we are replacing an existing file, we call one routine, if we are
- creating a new one, another routine */
- if(reply.sfReplacing)
- err = JuggleToExistingFile(&reply.sfFile, aJuggle);
- else
- err = JuggleToNewFile(&reply.sfFile, aJuggle);
-
- if(err == noErr)
- {
- /* Successful save, so rename the window, and set the dirty flag */
- SetWTitle(wind, reply.sfFile.name);
- (*aJuggle)->dirty = false;
-
- // Save the new file in the juggle, and signal success
- (*aJuggle)->fileSpec = reply.sfFile;
- success = true;
- }
- }
-
- return success;
- }
-
- // Called when the user selects "Page Setup..." from the File menu.
- void AppPageSetup(WindowPtr wind)
- {
- gxDialogResult result;
-
- if(wind == nil)
- return;
- DoGXFormatDialog(wind, &result);
- }
-
- // Called when the user selects "Print…" from the File menu.
- OSErr AppPrint(WindowPtr wind)
- {
- if(wind == nil)
- return -1;
- return DoGXPrinting(wind);
- }
-
- // Called when the user selects "Print One" from the File menu.
- OSErr AppPrintOne(WindowPtr wind)
- {
- gxShape pageShape;
- OSErr err = -1; // assume failure
- JuggleHandle aJuggle;
-
- /* Get the juggle in question */
- aJuggle = GetWindowJuggle(wind);
- if(aJuggle == nil)
- return err;
-
- // Make a page to print
- pageShape = MakePrintingShape(aJuggle);
- if(pageShape != nil)
- {
- // Call the shell to print it
- err = DoGXPrintOne(wind, pageShape);
-
- // Clean up
- GXDisposeShape(pageShape);
- }
-
- return err;
- }
-
- // Called when the user selects "Undo" from the Edit menu.
- void AppUndo(void)
- {
- }
-
- // Called when the user selects "Cut" from the Edit menu.
- void AppCut(void)
- {
- }
-
- // Called when the user selects "Copy" from the Edit menu.
- OSErr AppCopy(void)
- {
- return noErr;
- }
-
- // Called when the user selects "Paste" from the Edit menu.
- void AppPaste(void)
- {
- }
-
- // Called when the user selects "Clear" from the Edit menu.
- void AppClear(void)
- {
- }
-
- // Called when the shell is about to quit, saving has already happened.
- void AppCleanUp(void)
- {
- WindowPtr wind;
-
- // Close any windows (assumed already saved, if necessary),
- // which also kills the scrollBars
- while((wind = FrontWindow()) != nil)
- AppClose(wind);
-
- // If GX is active, kill it
- if(gGraphicsClient != nil)
- {
- // Clean up the global objects
- KillCommonGraphicObjects();
- TearDownGX();
- gGraphicsClient = nil;
- }
- }
-
- Boolean SetUpGX(void)
- {
- Boolean result = false; // assume failure
-
- // The GXNewGraphicsClient routine defines the graphics heap size. If you do not make this
- // call, the GX graphics engine will create this heap automatically. How? It will create
- // a heap which is a percentage of your application's ideal memory foot print. This call
- // allows you to explicity define the ammount of memory used by the graphics system for
- // its graphics objects heap.
-
- gGraphicsClient = GXNewGraphicsClient(nil, gGraphicsHeapSize * 1024, 0L);
-
- if ( gGraphicsClient )
- {
- // If gDebugging = TRUE, you will receive graphics library errors & notices will be posted.
- // This functionality will only work with the "debugging" version of QuickDraw GX. If you
- // don't have the debugging version installed, these functions will not work.
-
- if (gDebugging)
- {
- SetGraphicsLibraryErrors ();
- SetGraphicsLibraryNotices();
- }
-
- // Set gGiveMeValidation to TRUE if you want run-time validation. As you increase the amount
- // of validation, The drawing speed will SLOW down due to all of the internal checking.
-
- // gxPublicValidation will check parameters to public routines. For additional details
- // regarding the various levels of validation, see "Inside Macintosh: QuickDraw GX
- // Environment and Utilties."
-
- if (gGiveMeValidation)
- GXSetValidation(gxPublicValidation);
-
- // Initialize the new graphics and printing environments.
-
- if ( GXGetGraphicsError( nil ) != out_of_memory )
- {
- OSErr err;
-
- GXEnterGraphics();
- err = GXInitPrinting();
-
- // Initialize the other managers, if no errors occurred.
- if ( err == noErr )
- {
- InitCommonColors();
- // Initialize our printing event override UPP
- gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyGXPrintingEventOverride);
- // success!
- result = true;
- }
- }
- }
- return result;
- }
-
-
- void TearDownGX(void)
- {
- DisposeCommonColors();
- GXExitPrinting(); // Close the new printing mgr.
- GXExitGraphics(); // Deallocate all of the default structures
- GXDisposeGraphicsClient(gGraphicsClient);
- DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
- }
-
-
-